﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace MaJiang{
	public delegate bool BoolDelegateHuPai(CardManager cardMgr,PlayerCards player);
	#region Base
	public struct HuPaiData{
		public bool canHu;
		/// <summary>
		/// 胡牌类型
		/// </summary>
		public int huType;
		public string name;
		public static HuPaiData CantHuPai{
			get{
				HuPaiData data = new HuPaiData();
				data.canHu = false;
				data.huType = -1;
				data.name = "诈胡";
				return data;
			}
		}
	}
	public class HuPaiBase{
		public virtual string getName{
			get{
				return "诈胡";
			}
		}
		public HuPaiData tryHuPai (CardManager cardMgr, PlayerCards player){
			HuPaiData data = new HuPaiData();
			data.huType = getHuType;
			if(canHu(cardMgr,player))
				data.canHu = true;
			return data;
		}
		protected virtual bool canHu(CardManager cardMgr, PlayerCards player){
			return false;
		}
		protected virtual int getHuType{
			get{
				return 0;
			}
		}

	}
	public class HuPaiMgr{
		public HuPaiMgr(CardManager _cardMgr){
			cardMgr = _cardMgr;
		}
		public List<HuPaiBase> hupais = new List<HuPaiBase> ();
		public CardManager cardMgr;
		public void AddHuPai(HuPaiBase hufa){
			Debug.Log ("新增胡牌法:" + hufa.getName);
			hupais.Add (hufa);
		}
		public HuPaiData TryHuPai(PlayerCards player){
			for (var i = 0; i < hupais.Count; i++) {
				var result = hupais [i].tryHuPai (cardMgr, player);
				if(result.canHu)
					return result;
			}
			return HuPaiData.CantHuPai;
		}
		/// <summary>
		/// 设置胡牌玩法
		/// </summary>
		/// <param name="hupaiwanfa">Hupaiwanfa.</param>
		public void SetHuPaiWanFa(HuPaiWanFa hupaiwanfa){
			Debug.Log ("胡牌玩法:" + hupaiwanfa.getName+" 胡法数量:"+hupaiwanfa.hupais.Count);
			hupais.Clear ();
			for (var i = 0; i < hupaiwanfa.hupais.Count; i++) {
				AddHuPai (hupaiwanfa.hupais [i]);
			}
		}
	}
	public class LuaHuPai:HuPaiBase{
		public BoolDelegateHuPai checkCanHu;
		public int type;
		protected override int getHuType {
			get {
				return type;
			}
		}
		protected override bool canHu (CardManager cardMgr, PlayerCards player)
		{
			if (checkCanHu != null) {
				if (checkCanHu (cardMgr, player)) {
					return true;
				}
			}
			return base.canHu (cardMgr, player);
		}
	}
	public class HuPaiWanFa{
		public virtual string getName{
			get{
				return "诈胡";
			}
		}
		public List<HuPaiBase> hupais = new List<HuPaiBase>();
		public HuPaiWanFa(){
			AddHuPaiFa ();
		}
		public virtual void AddHuPaiFa(){
		}
		public T AddHuPai<T>()where T:HuPaiBase,new(){
			T t = new T ();
			AddHuPai (t);
			return t;
		}
		public void AddHuPai(HuPaiBase hupai){
			hupais.Add (hupai);
		}
	}
	#endregion

	#region 胡法
	/// <summary>
	///瞎几把胡 啥都可以胡
	/// </summary>
	public class AllCanHu:HuPaiBase{
		public override string getName {
			get {
				return "瞎几把胡";
			}
		}
		protected override bool canHu (CardManager cardMgr, PlayerCards player)
		{	
			var cachedCards = new List<Card> (player.cards);
			for(var i =0;i <cachedCards.Count;i++){
				
			}
			return true;
		}
	}
	#endregion
	#region 玩法
	/// <summary>
	/// 福州胡牌法
	/// </summary>
	public class HUPAI_FUZHOU:HuPaiWanFa{
		public override string getName {
			get {
				return "胡牌方法:福州";
			}
		}
		public override void AddHuPaiFa ()
		{
			base.AddHuPaiFa ();
			AddHuPai<AllCanHu> ();
		}
	}
	#endregion
}